/**
 * Created by hiddenlogic on 16/5/13.
 */
$.extend($.fn.validatebox.defaults.rules, {
    Number: {
        validator: function (value) {
            var reg = /^[0-9]*$/;
            return reg.test(value);
        },
        message: '请输入数字'
    },
    Mobile: {
        validator: function (value) {
            var reg = /^1[3|4|5|8|9]\d{9}$/;
            return reg.test(value);
        },
        message: '输入手机号码格式不准确.'
    },
    BlowHundred: {
        validator: function (value) {
            return value > 0 && value <= 100 ;
        },
        message: '请输入0-100数字'
    },
    ChineseValid:{
        validator:function (value){
            var reg=/^[\u4e00-\u9fa5]*$/;
            return reg.test(value);
        },
        message: '请输入汉字'
    },
    EnglishValid:{
        validator: function (value){
            var reg=/^[a-zA-Z]*$/;
            return reg.test(value);
        },
        message: '请输入英文'
    },
    CharValid:{
        validator: function (value){
            var reg=/^[,.?!,. \r\n]*$/;
            return reg.test(value);
        },
        message: '请输入指定字符'
    },
    IdCardValid: {
        validator: function(value){
            return isIdCardNo(value);
        },
        message: '不是有效的身份证号码'
    }
});

function isIdCardNo(num)
{
    var factorArr = new Array(7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2,1);
    var varArray = new Array();
    var lngProduct = 0;
    var intCheckDigit;
    var intStrLen = num.length;
    var idNumber = num;
    if ((intStrLen != 15) && (intStrLen != 18)) {
        return false;
    }
    for( var i=0;i<intStrLen;i++) {
        varArray[i] = idNumber.charAt(i);
        if ((varArray[i] < '0' || varArray[i] > '9') && (i != 17)) {
            return false;
        } else if (i < 17) {
            varArray[i] = varArray[i]*factorArr[i];
        }
    }
    if (intStrLen == 18) {
        var date8 = idNumber.substring(6,14);
        if (isDate8(date8) == false) {
            //error = "身份证中日期信息不正确！.";
            return false;
        }
        for(i=0;i<17;i++) {
            lngProduct = lngProduct + varArray[i];
        }
        intCheckDigit = 12 - lngProduct % 11;
        switch (intCheckDigit) {
            case 10:
                intCheckDigit = 'X';
                break;
            case 11:
                intCheckDigit = 0;
                break;
            case 12:
                intCheckDigit = 1;
                break;
        }
        if (varArray[17].toUpperCase() != intCheckDigit) {
            //error = "身份证效验位错误!...正确为： " + intCheckDigit + ".";
            return false;
        }
    }
    else{
        var date6 = idNumber.substring(6,12);
        if (isDate6(date6) == false) {
            //alert("身份证日期信息有误！.");
            return false;
        }
    }
    return true;
}



//根据url设置comboxId控件中combox的值
function  getComboboxValue(urlValue,comboxId,fieldValue,fieldText){
    $.ajax({
        type: "post",
        url: urlValue,
        dataType: "json",
        success:function(comboxResult) {
                $(comboxId).combobox({
                    data:comboxResult,
                    valueField:fieldValue,
                    textField:fieldText
                });
        }
    });
}
//重写format方法
Date.prototype.format = function(format) {
    var o = {
        "M+" : this.getMonth() + 1, // month
        "d+" : this.getDate(), // day
        "h+" : this.getHours(), // hour
        "m+" : this.getMinutes(), // minute
        "s+" : this.getSeconds(), // second
        "q+" : Math.floor((this.getMonth() + 3) / 3), // quarter
        "S" : this.getMilliseconds()
        // millisecond
    };

    if (/(y+)/.test(format)) {
        format = format.replace(RegExp.$1, (this.getFullYear() + "")
            .substr(4 - RegExp.$1.length));
    }

    for ( var k in o) {
        if (new RegExp("(" + k + ")").test(format)) {
            format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k]
                : ("00" + o[k]).substr(("" + o[k]).length));
        }
    }
    return format;
};

